home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The 640 MEG Shareware Studio 2
/
The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO
/
clang
/
nn.zip
/
EXECUTE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-12-31
|
3KB
|
159 lines
#include <signal.h>
#include <errno.h>
#include "config.h"
#include "term.h"
export int shell_restrictions = 0; /* disable shell escapes */
char *user_shell;
static shell_check()
{
if (shell_restrictions) {
msg("Restricted operation - not allowed");
return -1;
}
return 0;
}
init_execute()
{
if ((user_shell = getenv("SHELL")) == NULL)
user_shell = SHELL;
}
execute(path, args)
char *path, **args;
{
int was_raw, pid, i, status;
sig_type (*quit)(), (*intr)(), (*tstp)();
extern int errno;
was_raw = unset_raw();
while ((pid = fork()) == -1) sleep(1);
if (pid == 0) {
for (i = 3 ; i < 20 ; i++)
close(i);
execv(path, args);
fprintf(stderr, "%s: not found\n", path);
nn_exit(20);
}
quit = signal(SIGQUIT, SIG_IGN);
intr = signal(SIGINT, SIG_IGN);
#ifdef HAVE_JOBCONTROL
tstp = signal(SIGTSTP, SIG_DFL);
#endif
while ((i = wait(&status)) != pid && (i != -1 || errno == EINTR));
signal(SIGQUIT, quit);
signal(SIGINT, intr);
#ifdef HAVE_JOBCONTROL
signal(SIGTSTP, tstp);
#endif
if (was_raw) raw();
return status != 0;
}
shell_escape()
{
static char command[FILENAME] = "";
char *cmd;
int first = 1;
if (shell_check()) return 0;
prompt("!");
again:
cmd = get_s(command, NONE, NONE, NO_COMPLETION);
if (cmd == NULL) return !first;
strcpy(command, cmd);
if (!run_shell(command, first)) return !first;
first = 0;
if (any_key(0) == '!') { /* should use key map here */
putchar(CR);
putchar('!');
clrline();
goto again;
}
return 1;
}
static char *exec_sh_args[] = {
"nnsh",
"-c",
(char *)NULL, /* cmdstring */
(char *)NULL
};
run_shell(command, clear)
char *command;
int clear; /* -1 => no output, 0 => CR/NL, 1 => clear */
{
char cmdstring[512];
if (shell_check()) return 0;
if (!expand_file_name(cmdstring, command))
return 0;
if (clear > 0) {
clrdisp();
fl;
} else if (clear == 0) {
putchar(CR);
putchar(NL);
}
exec_sh_args[2] = cmdstring;
execute(user_shell, exec_sh_args);
return 1;
}
#ifndef HAVE_JOBCONTROL
static char *exec_suspend_args[] = {
"nnsh",
"-i",
(char *)NULL
};
#endif
suspend_nn()
{
int was_raw;
if (shell_check()) return 0;
was_raw = unset_raw();
gotoxy(0, Lines-1);
clrline();
visual_off();
#ifdef HAVE_JOBCONTROL
kill(process_id, SIGSTOP);
#else
execute(user_shell, exec_suspend_args);
#endif
visual_on();
s_redraw++;
if (was_raw) raw();
return 1;
}